// pages/myCenter/tradingAreaDetail.js
const controller = require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponBagId: "",
    detail: "",
    orderNo: "",
    state: 0,
    stateList: [],
    selectIndex: 0
  },

  getTabNum() {
    let list = this.data.stateList
    let value1 = {}
    let value2 = {}
    value1.couponBagId = this.data.couponBagId
    value1.orderNo = this.data.orderNo
    value2.couponBagId = this.data.couponBagId
    value2.orderNo = this.data.orderNo
    value1.state = 0
    value2.state = 1
    controller.getAccountBagDetail(value1).then(res => {
      list[0].num = res.data.goodsList.length
      this.setData({
        stateList: list
      })
    })
    controller.getAccountBagDetail(value2).then(res => {
      list[1].num = res.data.goodsList.length
      this.setData({
        stateList: list
      })
    })
  },

  //切换状态
  changeState(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectIndex: index,
      state: this.data.stateList[index].state
    })
    this.getBagDetail()
  },

  toDetail(e) {
    // if(this.data.detail.timeState==2){

    //   return
    // }
    // if(e.currentTarget.dataset.num==0){
    //   return
    // }
  

    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    let couponBagId = this.data.couponBagId
    let couponBagGoodsId = e.currentTarget.dataset.ids

    wx.navigateTo({
      url: '/pages/myCenter/shopGoodsDetail?id=' + id + "&shopId=" + shopId + "&couponBagId=" + couponBagId + "&couponBagGoodsId=" + couponBagGoodsId,
    })

    // wx.requestSubscribeMessage({
    //   tmplIds:['DlEG_b9R6f2bWjrW-jEmsvhWwi5EwtF_vWG-pr3ZIds','DlEG_b9R6f2bWjrW-jEmsiB1UqeXvcEPjhoDA0azFXg'],
    //   success:res=>{
    //     console.log(res)
    //   },
    //   complete:()=>{
    //     wx.navigateTo({
    //       url: '/pages/myCenter/shopGoodsDetail?id='+id+"&shopId="+shopId+"&couponBagId="+couponBagId+"&couponBagGoodsId="+couponBagGoodsId,
    //     })
    //   }
    // })
  },

  getBagDetail() {
    let value = {}
    value.orderNo = this.data.orderNo
    value.couponBagId = this.data.couponBagId
    value.state = this.data.state
    controller.getAccountBagDetail(value).then(res => {
      this.setData({
        detail: res.data
      })
    })
  },
  //用户持有券详情(爆品)
  getGoodsBagDetail(accountCouponBagId) {
    controller.couponBagDetail({
      accountCouponBagId
    }).then(res => {
      if(res.data.effectiveTimeState){
        res.data.effectiveEndTime = this.handleTime(res.data.effectiveEndTime)
        res.data.effectiveStartTime = this.handleTime(res.data.effectiveStartTime)
      }
      
      this.setData({
        detail: res.data
      })
    })
  },

  handleTime(time){
    let str = ''
    const strArr = ['年','月','日']
    let arr = time.substr(0,10).split('-')
    for(let i = 0;i<arr.length;i++){
      str = str.concat(arr[i] + strArr[i])
    }
    return str
  },

  //跳转店铺详情
  toShop(e) {
    let shopId = e.currentTarget.dataset.shopid
    console.log(shopId)
    wx.reLaunch({
      url: '/pages/Index/Index?shopId=' + shopId,
    })
  },
  back() {
    wx.navigateBack({
      delta: 0,
    })
  },
  phone(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  //跳转商品详情
  toGoodsDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    
    wx.navigateTo({
      url: '/pages/Index/Detail?id='+id+"&shopId="+shopId,
    })
  },
  
  //  跳转播放视频页
  toVideoPage() {
    const data = {
      video: this.data.detail.video,
      videoDesc: this.data.detail.videoDesc || '',
      img: this.data.detail.img || this.data.detail.image ,
      name: this.data.detail.name ,

      type: this.data.detail.type,
      couponBagId: this.data.detail.couponBagId,

      isFromMyCenterVideo:true
    }
    wx.navigateTo({
      url: '/pages/TradingArea/videoPlay?data=' + JSON.stringify(data),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      couponBagId: options.id,
      orderNo: options.orderNo,
      type: options.type,
    })
    if (this.data.type == 2) {
      this.getGoodsBagDetail(options.accountCouponBagId)
    } else {
      this.getTabNum()
      this.getBagDetail()
    }
    if (this.data.type == 2) {
      console.log(1)
      this.setData({
        stateList: [{
            name: "可用爆品",
            state: 0,
            num: 0
          },
          {
            name: "历史爆品",
            state: 1,
            num: 0
          }
        ],
      })
    } else if (this.data.type == 5) {
      this.setData({
        stateList: [{
            name: "可用礼包商品",
            state: 0,
            num: 0
          },
          {
            name: "历史礼包商品",
            state: 1,
            num: 0
          }
        ],
      })
    } else {
      console.log(2)
      this.setData({
        stateList: [{
            name: "可用拼团商品",
            state: 0,
            num: 0
          },
          {
            name: "历史拼团商品",
            state: 1,
            num: 0
          }
        ],
      })
    }
  },
  onShow: function () {

  },


})